package com.hawk.eye.server.service.impl;

import com.hawk.eye.framework.utils.ExceptionUtils;
import com.hawk.eye.common.utils.StringUtil;
import com.hawk.eye.framework.utils.SecurityUtils;
import com.hawk.eye.server.convert.DictConvert;
import com.hawk.eye.server.domain.dto.req.DictReqDTO;
import com.hawk.eye.server.domain.dto.resp.DictDataRespDTO;
import com.hawk.eye.server.mapper.DictMapper;
import com.hawk.eye.server.service.IDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Service业务层处理
 *
 * @author salad
 * @date 2023-09-23
 */
@Service
@RequiredArgsConstructor
public class DictServiceImpl implements IDictService {

    private final DictMapper dictMapper;


    @Override
    public List<DictDataRespDTO> dictDataList(DictReqDTO reqDTO) {
        return DictConvert.INSTANCE.to4(dictMapper.selectDictDataList(reqDTO));
    }

    @Override
    public boolean saveDict(DictReqDTO reqDTO) {
        if (StringUtil.isNotBlank(reqDTO.getValue())) {
            long count = dictMapper.countDictDataByValue(reqDTO);
            ExceptionUtils.isTrueThrow(count > 0, "键值[{}]已存在", reqDTO.getValue());
        }
        reqDTO.setCreateUser(SecurityUtils.getUserId());
        reqDTO.setUpdateUser(SecurityUtils.getUserId());
        dictMapper.insertDict(reqDTO);
        return true;
    }


    @Override
    public boolean updateDict(DictReqDTO reqDTO) {
        if (StringUtil.isNotBlank(reqDTO.getValue())) {
            long count = dictMapper.countDictDataByValue(reqDTO);
            ExceptionUtils.isTrueThrow(count > 0, "键值[{}]已存在", reqDTO.getValue());
        }
        reqDTO.setUpdateUser(SecurityUtils.getUserId());
        dictMapper.updateDict(reqDTO);
        return true;
    }

    @Override
    public boolean removeDict(Long[] ids) {
        dictMapper.updateDictByIds(ids);
        return true;
    }

    @Override
    public List<DictDataRespDTO> dictListByType(String type) {
        return dictMapper.dictListByType(type);
    }

}
